package com.pwrd.minichat;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;

/**
 * @Auther syc.
 * @Date 2016/10/31 14:58.
 */
public class ChatClient extends Frame {

    TextField tfTxt = new TextField();
    TextArea taContent = new TextArea();
    Socket socket;
    DataOutputStream dos;
    DataInputStream dis;
    boolean connected = false;
    Thread recvThread = new Thread(new RecvListener());


    public static void main(String[] args) {
        new ChatClient().launchFram();
    }

    public void launchFram() {
        setLocation(400, 300);
        this.setSize(300, 300);
        add(tfTxt, BorderLayout.SOUTH);
        add(taContent, BorderLayout.NORTH);
        pack();
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                disconnect();
                System.exit(0);
            }
        });
        tfTxt.addActionListener(new TFListener());
        setVisible(true);
        connect();
        recvThread.start();
    }

    public void connect() {
        try {
            socket = new Socket("localhost", 8888);
            dos = new DataOutputStream(socket.getOutputStream());
            System.out.println("This client has connected to server");

            dis = new DataInputStream(socket.getInputStream());

            connected = true;

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void disconnect() {
        try {
            connected = false;

            if (dos != null) dos.close();
            if (dis != null) dis.close();
            if (socket != null) socket.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private class TFListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            String s = tfTxt.getText().trim();
            tfTxt.setText("");
            try {
                dos.writeUTF(s);
                dos.flush();


            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }

    private class RecvListener implements Runnable {

        @Override
        public void run() {

            try {
                while (connected) {
                    String instr = dis.readUTF();
                    taContent.setText(taContent.getText() + instr + "\n");
                }
            } catch (SocketException se) {
                System.out.println("client closed");
            } catch (EOFException e) {
                System.out.println("client closed");
            } catch (IOException e) {
                e.printStackTrace();
            }


        }
    }
}
